package com.coderman.common.starter.excelport.imports.service.common;

import com.alibaba.fastjson.JSON;
import com.coderman.common.starter.excelport.imports.annotations.ImportField;
import com.coderman.common.starter.excelport.imports.annotations.ImportTitle;
import com.coderman.common.starter.excelport.imports.beans.ImportFieldBean;
import com.coderman.common.starter.excelport.imports.beans.ImportTitleBean;
import com.coderman.common.starter.excelport.imports.beans.PrimaryKeyBean;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Description:
 * date: 2020/9/15 9:14 上午
 *
 * @author fanchunshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Service
public class ReadExcelService {
    private Logger logger = LoggerFactory.getLogger(ReadExcelService.class);

    /**
     * 获取导入数据模型
     *
     * @param clazz
     * @return
     */
    public ImportTitleBean getImportTitle(Class<?> clazz) {
        ImportTitleBean importTitleBean = new ImportTitleBean();
        if (clazz.isAnnotationPresent(ImportTitle.class)) {
            ImportTitle importTitle = clazz.getAnnotation(ImportTitle.class);
            if (StringUtils.isNotEmpty(importTitle.title())) {
                importTitleBean.setTitle(importTitle.title());
            }
            if (importTitle.mainArr().length > 0) {
                importTitleBean.setMainArr(importTitle.mainArr());
            }
            if (importTitle.childArr().length > 0) {
                importTitleBean.setChildArr(importTitle.childArr());
            }
            if (importTitle.referIdArr().length > 0) {
                importTitleBean.setReferIdArr(importTitle.referIdArr());
            }
        }
        String [] mainArr = importTitleBean.getMainArr();
        String [] referIdArr = importTitleBean.getReferIdArr();

        ConcurrentHashMap<String, PrimaryKeyBean> primaryKeyBeanMap = new ConcurrentHashMap<>();
        for (String mainTable : mainArr){
            if(StringUtils.isNotEmpty(mainTable)){
                PrimaryKeyBean primaryKeyBean = new PrimaryKeyBean();
                primaryKeyBean.setChildTableMap(new HashMap<>());
                primaryKeyBeanMap.put(mainTable,primaryKeyBean);
            }
        }

        for (String referId : referIdArr){
            if(StringUtils.isEmpty(referId)){
                continue;
            }
            String [] idInfo = referId.split("=");
            String mainTable = idInfo[0];
            String childTable = idInfo[1];
            String [] main = mainTable.split("\\.");
            primaryKeyBeanMap.get(main[0]).setMainTableName(main[0]);
            primaryKeyBeanMap.get(main[0]).setMainTableIdField(main[1]);
            String [] child = childTable.split("\\.");
            primaryKeyBeanMap.get(main[0]).getChildTableMap().put(child[0],child[1]);
        }
        logger.info("importTitleBean = "+ JSON.toJSONString(importTitleBean));

        return importTitleBean;
    }

    /**
     * 反射获取导入模型中的列字段名称
     *
     * @param clazz
     * @return
     * @throws NoSuchMethodException
     * @throws java.lang.reflect.InvocationTargetException
     * @throws IllegalAccessException
     */
    public ConcurrentHashMap<String, ImportFieldBean> getMetaFieldColumnMap(Class<?> clazz) {

        Field[] fields = clazz.getDeclaredFields();
        ConcurrentHashMap<String, ImportFieldBean> metaMap = new ConcurrentHashMap<>(fields.length);

        for (int i = 0; i < fields.length; ++i) {
            Field f = fields[i];
            if (f.isAnnotationPresent(ImportField.class)) {
                ImportField importField = f.getAnnotation(ImportField.class);
                ImportFieldBean importFieldBean = new ImportFieldBean();
                importFieldBean.setFormate(importField.formate());
                importFieldBean.setIndex(importField.index());
                importFieldBean.setSourceKey(importField.sourceKey());
                importFieldBean.setTitle(importField.title());
                importFieldBean.setUnique(importField.isUnique());
                importFieldBean.setColumnName(importField.columnName());
                importFieldBean.setReferFieldIndex(importField.referFieldIndex());
                metaMap.put(f.getName(), importFieldBean);
            }
        }

        return metaMap;
    }


}
